﻿using DSPL.Common.ViewModel;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
/*-------------------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * Date : January 2015
 * TxtReferenceNo : Common text box for transaction reference number
 -------------------------------------------------------------*/

using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    public class TxtReferenceNo : TextBox
    {
        //static TxtReferenceNo()
        //{
        //    DefaultStyleKeyProperty.OverrideMetadata(typeof(TxtReferenceNo), new FrameworkPropertyMetadata(typeof(TxtReferenceNo)));
        //}

        public TxtReferenceNo()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                ResolveDependancies();
            }
        }

        #region Declaring Properties

        #region General Properties

        IMessageService MessageService;
        IReadService ReadService;

        public HelpNames HelpName
        {
            get { return (HelpNames)GetValue(HelpNameProperty); }
            set { SetValue(HelpNameProperty, value); }
        }

        public HelpNames TempHelpName
        {
            get { return (HelpNames)GetValue(TempHelpNameProperty); }
            set { SetValue(TempHelpNameProperty, value); }
        }

        public HelpNames AlternativeHelpName
        {
            get { return (HelpNames)GetValue(AlternativeHelpNameProperty); }
            set { SetValue(AlternativeHelpNameProperty, value); }
        }

        public HelpNames TempAlternativeHelpName
        {
            get { return (HelpNames)GetValue(TempAlternativeHelpNameProperty); }
            set { SetValue(TempAlternativeHelpNameProperty, value); }
        }

        public object KeyValue
        {
            get { return (object)GetValue(KeyValueProperty); }
            set { SetValue(KeyValueProperty, value); }
        }

        public string KeyFieldName
        {
            get { return (string)GetValue(KeyFieldNameProperty); }
            set { SetValue(KeyFieldNameProperty, value); }
        }

        public string KeyFieldType
        {
            get { return (string)GetValue(KeyFieldTypeProperty); }
            set { SetValue(KeyFieldTypeProperty, value); }
        }

        public DataRow SelectedDataRow
        {
            get { return (DataRow)GetValue(SelectedDataRowProperty); }
            set { SetValue(SelectedDataRowProperty, value); }
        }

        public Modes ParentFormMode
        {
            get { return (Modes)GetValue(ParentFormModeProperty); }
            set { SetValue(ParentFormModeProperty, value); }
        }

        public string FilterString
        {
            get { return (string)GetValue(FilterStringProperty); }
            set { SetValue(FilterStringProperty, value); }
        }

        public string TempFilterString
        {
            get { return (string)GetValue(TempFilterStringProperty); }
            set { SetValue(TempFilterStringProperty, value); }
        }

        public string ParentTableName
        {
            get { return (string)GetValue(ParentTableNameProperty); }
            set { SetValue(ParentTableNameProperty, value); }
        }

        public string CodeFieldName
        {
            get { return (string)GetValue(CodeFieldNameProperty); }
            set { SetValue(CodeFieldNameProperty, value); }
        }

        public bool IsLoadApprovedData
        {
            get { return (bool)GetValue(IsLoadApprovedDataProperty); }
            set { SetValue(IsLoadApprovedDataProperty, value); }
        }


        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty KeyValueProperty = DependencyProperty.Register("KeyValue", typeof(object), typeof(TxtReferenceNo), new PropertyMetadata(null, KeyValuePropertyChangedCallback));

        public static readonly DependencyProperty KeyFieldNameProperty = DependencyProperty.Register("KeyFieldName", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty KeyFieldTypeProperty = DependencyProperty.Register("KeyFieldType", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedDataRowProperty = DependencyProperty.Register("SelectedDataRow", typeof(DataRow), typeof(TxtReferenceNo), new PropertyMetadata(null));

        public static readonly DependencyProperty HelpNameProperty = DependencyProperty.Register("HelpName", typeof(HelpNames), typeof(TxtReferenceNo), new PropertyMetadata(HelpNames.Default));

        public static readonly DependencyProperty TempHelpNameProperty = DependencyProperty.Register("TempHelpName", typeof(HelpNames), typeof(TxtReferenceNo), new PropertyMetadata(HelpNames.Default));

        public static readonly DependencyProperty AlternativeHelpNameProperty = DependencyProperty.Register("AlternativeHelpName", typeof(HelpNames), typeof(TxtReferenceNo), new PropertyMetadata(HelpNames.Default));

        public static readonly DependencyProperty TempAlternativeHelpNameProperty = DependencyProperty.Register("TempAlternativeHelpName", typeof(HelpNames), typeof(TxtReferenceNo), new PropertyMetadata(HelpNames.Default));

        public static readonly DependencyProperty ParentFormModeProperty = DependencyProperty.Register("ParentFormMode", typeof(Modes), typeof(TxtReferenceNo), new PropertyMetadata(Modes.Undo, ParentFormModeChangedCallBack));

        public static readonly DependencyProperty FilterStringProperty = DependencyProperty.Register("FilterString", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty TempFilterStringProperty = DependencyProperty.Register("TempFilterString", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty ParentTableNameProperty = DependencyProperty.Register("ParentTableName", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty CodeFieldNameProperty = DependencyProperty.Register("CodeFieldName", typeof(string), typeof(TxtReferenceNo), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty IsLoadApprovedDataProperty = DependencyProperty.Register("IsLoadApprovedData", typeof(bool), typeof(TxtReferenceNo), new PropertyMetadata(false));

        #endregion

        #endregion

        #region Property Changedback events

        private static void ParentFormModeChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TxtReferenceNo objTxtReferenceNo = d as TxtReferenceNo;

            if (objTxtReferenceNo != null)
            {
                Modes newMode = (Modes)e.NewValue;
                objTxtReferenceNo.EnableDisableControl(newMode);
            }
        }

        private static void KeyValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TxtReferenceNo objTxtReferenceNo = d as TxtReferenceNo;

            if (objTxtReferenceNo != null)
            {
                object newKeyValue = e.NewValue;
                objTxtReferenceNo.LoadCurrentDataRow(newKeyValue);
            }
        }

        #endregion

        #region Control's inbuilt events

        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            LoadHelpWindow();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Key == Key.F1)
                LoadHelpWindow();
            else if (e.Key == Key.Enter || e.Key == Key.Return)
                LoadHelpWindowbyCode();
        }

        #endregion

        #region Private events

        private void ResolveDependancies()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                MessageService = container.Resolve<IMessageService>();
                ReadService = container.Resolve<IReadService>();
            }
        }

        private void LoadHelpWindow()
        {
            ListWindowViewModel vmLTxnHelpWindow = new ListWindowViewModel();
            if (vmLTxnHelpWindow != null)
            {
                if (IsLoadApprovedData)
                {
                    vmLTxnHelpWindow.HelpName = this.HelpName;
                    vmLTxnHelpWindow.FilterString = FilterString;
                }
                else
                {
                    vmLTxnHelpWindow.HelpName = this.TempHelpName;
                    vmLTxnHelpWindow.FilterString = TempFilterString;
                }
                vmLTxnHelpWindow.Initialize();
                MessageService.ShowPopupWindow(vmLTxnHelpWindow);
                if (vmLTxnHelpWindow.SelectedRow != null)
                    KeyValue = vmLTxnHelpWindow.SelectedRow[KeyFieldName];
            }
        }

        private void LoadHelpWindowbyCode()
        {
            if (!string.IsNullOrEmpty(CodeFieldName) && this.Text != null && !string.IsNullOrEmpty(this.Text))
            {
                ListWindowViewModel vmLTxnHelpWindow = new ListWindowViewModel();
                if (vmLTxnHelpWindow != null)
                {
                    if (!IsLoadApprovedData)
                    {
                        vmLTxnHelpWindow.HelpName = this.HelpName;
                        vmLTxnHelpWindow.AlternativeHelpName = this.AlternativeHelpName;
                        vmLTxnHelpWindow.FormMode = ParentFormMode;
                        vmLTxnHelpWindow.FilterString = FilterString;
                    }
                    else
                    {
                        vmLTxnHelpWindow.HelpName = this.TempHelpName;
                        vmLTxnHelpWindow.AlternativeHelpName = this.TempAlternativeHelpName;
                        vmLTxnHelpWindow.FormMode = ParentFormMode;
                        vmLTxnHelpWindow.FilterString = TempFilterString;

                    }


                    vmLTxnHelpWindow.FilterColumn = CodeFieldName;
                    vmLTxnHelpWindow.FilterValue = this.Text;
                    vmLTxnHelpWindow.FilterType = "STRING";
                    vmLTxnHelpWindow.FilterEquation = QComparison.LIKE;
                    vmLTxnHelpWindow.FormMode = ParentFormMode;
                    vmLTxnHelpWindow.Initialize();

                    if (vmLTxnHelpWindow.RowCount > 1 && KeyValue == null)
                        MessageService.ShowPopupWindow(vmLTxnHelpWindow);

                    if (vmLTxnHelpWindow.SelectedRow != null)
                        KeyValue = vmLTxnHelpWindow.SelectedRow[KeyFieldName];

                    if (vmLTxnHelpWindow.RowCount == 0)
                    {
                        ClearControls();
                    }


                }
            }
            else
                ClearControls();
        }

        private void ClearControls()
        {
            KeyValue = null;
            this.Text = string.Empty;
        }

        private void EnableDisableControl(Modes newMode)
        {
            switch (newMode)
            {
                case Modes.New:
                case Modes.None:
                case Modes.Save:
                case Modes.Undo:
                case Modes.First:
                case Modes.Previous:
                case Modes.Next:
                case Modes.Last:
                    this.IsEnabled = false;
                    break;
                case Modes.Edit:
                case Modes.Delete:
                case Modes.Approve:
                case Modes.Print:
                    this.IsEnabled = true;
                    break;

            }
        }

        private void LoadCurrentDataRow(object newKeyValue)
        {
            if (newKeyValue != null)
            {
                DataTable dtTxn = ReadService.GetParentTransactionData(ParentTableName, KeyFieldName, newKeyValue);
                if (dtTxn != null && dtTxn.Rows.Count > 0)
                    SelectedDataRow = dtTxn.Rows[0];
            }
        }

        #endregion

    }
}
